package com.fg.cloud.factory.controller.factory;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.FactoryAddressEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryAddressVo;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.service.factory.FactoryAddressService;

/**
 * Description:厂商地址管理
 * @author dingjiang
 * @date 2017年9月18日上午9:58:49
 */
@Controller
@RequestMapping("/facAddress")
public class FactoryAddressController extends BaseController {

	@Autowired
	private FactoryAddressService facAddressService;
	
	/**
	 * Description:新增地址
	 * @param req
	 * @param resp
	 * @param address
	 * @author dingjiang
	 * @date 2017年9月18日上午10:49:48
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public void insert(HttpServletRequest req, HttpServletResponse resp, FactoryAddressEntity address) {
		try {
			FactoryUserVo user = getCurrentFactoryUser();
			//验证关键参数非空
			if ( null==address.getReceiver() || StringUtils.isEmpty(address.getReceiver()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请填写收货人");
				return ;
			}
			if ( null==address.getAddress() || StringUtils.isEmpty(address.getAddress()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请填写详细地址");
				return ;
			}
			if ( null==address.getPhone() || StringUtils.isEmpty(address.getPhone()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请填写联系电话");
				return ;
			}
			//判重
			address.setFacId( user.getFacId() );
			if ( facAddressService.judgeIsExit(address) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "该地址已存在");
				return ;
			}
			boolean flag = facAddressService.insertAdd(address,user);
			doResult(resp, flag, flag?StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, null, 
					flag?ResultType.SAVE_SUCCESS:ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:编辑保存地址
	 * @param req
	 * @param resp
	 * @param address
	 * @author dingjiang
	 * @date 2017年9月18日下午2:09:12
	 */
	@RequestMapping("/editor")
	@ResponseBody
	public void editor(HttpServletRequest req, HttpServletResponse resp, FactoryAddressEntity address) {
		try {
			FactoryUserVo user = getCurrentFactoryUser();
			//验证ID非空
			if ( LongUtils.isEmpty(address.getId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "ID不能为空");
				return ;
			}
			//验证ID合法
			if ( !facAddressService.validateId(address.getId(), user.getFacId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "ID填写有误");
				return ;
			}
			FactoryAddressVo find = facAddressService.find(address.getId());
			//必要参数不能为空
			if ( null==address.getReceiver() || StringUtils.isEmpty(address.getReceiver()) )
				address.setReceiver(find.getReceiver());
			if ( null==address.getAddress() || StringUtils.isEmpty(address.getAddress()) )
				address.setAddress(find.getAddress());
			if ( null==address.getPhone() || StringUtils.isEmpty(address.getPhone()) )
				address.setPhone(find.getPhone());
			//判重
			if ( facAddressService.JudgeIsExit(address) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "该地址已存在");
				return ;
			}
			boolean flag = facAddressService.editor(address, user, find);
			doResult(resp, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null, 
					flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}	
	}
	
	/**
	 * Description:设置默认地址
	 * @param req
	 * @param resp
	 * @param address
	 * @author dingjiang
	 * @date 2017年9月18日下午4:19:36
	 */
	@RequestMapping(value="/setDefault",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void setDefault(HttpServletRequest req , HttpServletResponse resp , Long id){
		try {
			FactoryUserVo user = getCurrentFactoryUser();
			//验证ID非空
			if ( LongUtils.isEmpty(id) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "ID不能为空");
				return ;
			}
			//验证ID合法
			if ( !facAddressService.validateId(id, user.getFacId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "ID填写有误");
				return ;
			}
			
			boolean flag = facAddressService.setDefault(id, user);
			doResult(resp, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null, 
					flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:批量删除（物理删除）
	 * @param req
	 * @param resp
	 * @param ids
	 * @author dingjiang
	 * @date 2017年9月19日下午5:07:37
	 */
	@RequestMapping(value="/deletes",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void deletes(HttpServletRequest req , HttpServletResponse resp , @Param("ids")String ids){
		try {
			if (StringUtils.isEmpty(ids)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return ;
			}
			String[] strs = ids.split(",");
			Long[] arrays = new Long[strs.length];
			for(int i = 0; i < strs.length; i++){
				arrays[i] = Long.parseLong(strs[i]);
		    }
			boolean flag = facAddressService.deletes(getCurrentFactoryUser().getFacId(), arrays);
			doResult(resp, flag, flag ? StatusCode.REMOVESUCCESS : StatusCode.REMOVEERROR, null, 
					flag ? ResultType.REMOVE_SUCCESS : ResultType.REMOVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
		}
	}
	
	/**
	 * Description:禁用地址
	 * @param req
	 * @param resp
	 * @param ids
	 * @author dingjiang
	 * @date 2017年9月18日下午5:18:45
	 */
	@RequestMapping(value="/disable",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void disable(HttpServletRequest req , HttpServletResponse resp , Long id){
		try {
			//验证ID非空
			if ( LongUtils.isEmpty(id) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "ID不能为空");
				return ;
			}
			Long facId = getCurrentFactoryUser().getFacId();
			//验证ID合法
			if ( !facAddressService.validateId(id, facId) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "ID填写有误");
				return ;
			}
			boolean flag = facAddressService.disable(facId, id);
			doResult(resp, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null, 
					flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
		}
	}


	/**
	 * Description:分页查询
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @author dingjiang
	 * @date 2017年9月18日下午12:33:36
	 */
	@RequestMapping(value="/selectPage",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectPage(HttpServletRequest req, HttpServletResponse resp,
			Integer page, Integer rows, String sort, String order){
		try {
			if (null == page) page = 1;
			if (null == rows) rows = 10;
			PageUtils<FactoryAddressVo> pageinfo = new PageUtils<>(page, rows, sort, order);
			pageinfo.getCondition().put("facId = ", getCurrentFactoryUser().getFacId());
			facAddressService.selectPage(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:查询所有厂商地址
	 * @param req
	 * @param resp
	 * @author dingjiang
	 * @date 2017年11月1日下午4:19:48
	 */
	@RequestMapping(value="/selectAll",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectAll(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Long facId = getCurrentFactoryUser().getFacId();
			List<FactoryAddressVo> list = facAddressService.selectAll(facId);
			doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);			
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
	

}
